home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / f_heap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  3.7 KB  |  168 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  f_heap.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #ifndef LEDA_FHEAP_H
  17. #define LEDA_FHEAP_H
  18.  
  19. //------------------------------------------------------------------------------
  20. //
  21. // Fibonacci Heap
  22. //
  23. // Michael Muth  (1988)
  24. //
  25. // Modifications
  26. //
  27. // - virtual compare function  (Stefan Naeher, August 1989)
  28. //
  29. // - virtual clear functions   (Stefan Naeher, April 1990)
  30. //
  31. //------------------------------------------------------------------------------
  32.  
  33.  
  34. #include <LEDA/basic.h>
  35.  
  36.  
  37. class f_heap_node  {
  38. // repraesentiert Knoten und Heap geordnete Baeume
  39.  
  40. friend class f_heap;
  41.  
  42.    f_heap_node* next;          // used to link all used items 
  43.    f_heap_node* pred;          
  44.  
  45.    f_heap_node* left;          // left and right siblings (circular List)
  46.    f_heap_node* right;   
  47.    f_heap_node* parent;        // parent node
  48.    f_heap_node* children;      // a child
  49.  
  50.    int  rank;                   // number of children
  51.    char mark;                   // ( 1=true, 0=false )
  52.  
  53.    GenPtr key;                  // key
  54.    GenPtr inf;                  // information
  55.  
  56.  
  57. // size: 10 words =  40 bytes
  58.  
  59.  
  60.    void link(f_heap_node*);
  61.    void cut(f_heap_node*);
  62.  
  63.  
  64. public:
  65.  
  66.    f_heap_node(GenPtr k, GenPtr i, f_heap_node* n) 
  67.    {  key = k;
  68.       inf = i;
  69.       next = n;
  70.       pred = 0;
  71.       rank = 0;
  72.       mark = 0;
  73.       parent = 0;
  74.       children = 0;
  75.  
  76.       if (n) n->pred = this;
  77.  
  78.     }  // end of f_heap_node()
  79.  
  80.  
  81.   LEDA_MEMORY(f_heap_node)
  82.  
  83.  };  //f_heap_node
  84.  
  85.  
  86. typedef f_heap_node* f_heap_item;
  87.  
  88.  
  89. class f_heap
  90.  /* Ein F-Heap enthaelt eine zirkulaere Liste von Heap geordneten
  91.     Baeumen. Der Einstieg erfolgt ueber den minptr, einen Zeiger
  92.     auf den Baum mit kleinstem Schluessel in der Wurzel. Die ei-
  93.     gentlichen Items ( Paare aus NxT ) sind in den Knoten der
  94.     Baeume enthalten.                                               */
  95.  { 
  96.    int node_count;         // number of nodes
  97.    f_heap_node* minptr;    // entry to the List of roots
  98.    f_heap_node* node_list; // List of all nodes
  99.  
  100.    int max_rank() const;
  101.  
  102.    f_heap_node* new_f_heap_node(GenPtr k, GenPtr i)
  103.    { return  node_list = new f_heap_node(k,i,node_list); }
  104.  
  105.    virtual int cmp(GenPtr x, GenPtr y) const { return compare(x,y); }
  106.  
  107.    virtual void print_key(GenPtr)  const {}
  108.    virtual void print_inf(GenPtr)  const {}
  109.  
  110.    virtual void clear_key(GenPtr&)  const {}
  111.    virtual void clear_inf(GenPtr&)  const {}
  112.  
  113.    virtual void copy_key(GenPtr&)   const {}
  114.    virtual void copy_inf(GenPtr&)   const {}
  115.  
  116.    virtual int  int_type() const { return 0; }
  117.  
  118. protected:
  119.  
  120.   f_heap_node* item(void* p) const { return (f_heap_node*)p; }
  121.  
  122. public:
  123.  
  124.  f_heap();
  125.  f_heap(const f_heap&);
  126.  f_heap& operator=(const f_heap&);
  127.  
  128. virtual ~f_heap()  { clear(); }
  129.  
  130.  
  131. f_heap_node* insert(GenPtr, GenPtr);
  132. f_heap_node* find_min() const { return(minptr); }
  133.  
  134. void del_min();  
  135. void decrease_key(f_heap_node*,GenPtr);
  136. void change_inf(f_heap_node*,GenPtr);
  137. void del_item(f_heap_node *x){ decrease_key(x,minptr->key); del_min();}
  138. void clear();
  139.  
  140. GenPtr  key(f_heap_node *x) const { return x->key ; }
  141. GenPtr  inf(f_heap_node *x) const { return x->inf; }
  142.  
  143.   
  144. int  size()  const { return node_count; }
  145. int  empty() const { return (find_min()==0) ? true : false; }
  146.  
  147.  
  148. // Iteration:
  149.  
  150.  
  151. f_heap_node* first_item() const;
  152.  
  153. f_heap_node* next_item(f_heap_node* p) const;
  154.  
  155.  
  156.  
  157. };  // end of class f_heap
  158.  
  159.  
  160. // dummy I/O and cmp functions
  161.  
  162. inline void Print(const f_heap&, ostream&) { }
  163. inline void Read (f_heap&, istream&) { }
  164. inline int  compare(const f_heap&,const f_heap&) { return 0; }
  165.  
  166. #endif
  167.